package cn.wolfcode.shop.web.validator;

import cn.wolfcode.shop.web.util.ValidatorUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by wolfcode-lanxw
 */
public class MobileValidator implements ConstraintValidator<IsMobile,String> {
    private boolean required;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        this.required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
       if(required){
           return ValidatorUtil.isMobile(value);
       }else{
           if(StringUtils.isEmpty(value)){
               return true;
           }else{
               return ValidatorUtil.isMobile(value);
           }
       }
    }
}
